﻿Imports System.Data.SqlClient
Public Class FrmTKPhieuXuat

    Private Sub FrmTKPhieuXuat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cbxKhachHang.DataSource = TruyVanDanhSach("select * from KhachHang")
        Me.cbxKhachHang.DisplayMember = "TenKH"
        Me.cbxKhachHang.ValueMember = "MaKH"
        Me.cbxKhachHang.SelectedIndex = -1
        UpdateList()
    End Sub

    Public Function TruyVanDanhSach(ByVal strSql As String) As DataTable
        Dim myConn As New SqlConnection(chuoiketnoi)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.Fill(myTable)
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return myTable
    End Function

    Private Sub UpdateList()
        Dim strSql As String = "select ROW_NUMBER() OVER (ORDER BY sc.NgayCT) as STT, sc.SoCT as [Số CT], sc.SoHD as [Số HĐ],"
        strSql &= " kh.TenKH as [Tên KH], convert(VARCHAR,cast(sc.TongTien as money),1) as [Tổng Tiền], convert(VARCHAR, NgayCT,103) as [Ngày CT], convert(VARCHAR, NgayHD,103) as [Ngày HĐ] from SoCai sc left join KhachHang kh on sc.MaKH = kh.MaKH where sc.LoaiCT = 'PXK' "
        Dim myConn As New SqlConnection(chuoiketnoi)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myComm As New SqlCommand()
            myComm.Connection = myConn
            If radSoCT.Checked Then
                strSql &= " and sc.SoCT like @SoCT "
                myComm.Parameters.AddWithValue("@SoCT", "%" & txtSoCT.Text & "%")
            ElseIf radSoHD.Checked Then
                strSql &= " and sc.SoHD like @SoHD "
                myComm.Parameters.AddWithValue("@SoHD", "%" & txtSoHD.Text & "%")
            ElseIf radNgayCT.Checked Then
                strSql &= " and (sc.NgayCT >= convert(DATETIME, @From, 103) and sc.NgayCT <= convert(DATETIME, @To, 103))"
                myComm.Parameters.AddWithValue("@From", dtpNgayCTFrom.Value.ToString("dd/MM/yyyy"))
                myComm.Parameters.AddWithValue("@To", dtpNgayCTTo.Value.ToString("dd/MM/yyyy"))
            ElseIf radNgayHD.Checked Then
                strSql &= " and (sc.NgayHD >= convert(DATETIME, @From, 103) and sc.NgayHD <= convert(DATETIME, @To, 103))"
                myComm.Parameters.AddWithValue("@From", dtpNgayHDFrom.Value.ToString("dd/MM/yyyy"))
                myComm.Parameters.AddWithValue("@To", dtpNgayHDTo.Value.ToString("dd/MM/yyyy"))
            Else
                If cbxKhachHang.SelectedValue IsNot Nothing Then
                    strSql &= " and sc.MaKH = @MaKH "
                    myComm.Parameters.AddWithValue("@MaKH", cbxKhachHang.SelectedValue)
                End If
            End If
            strSql &= " order by STT"
            myComm.CommandText = strSql
            Dim myAdapter As New SqlDataAdapter(myComm)
            myAdapter.Fill(myTable)
            dgvList.DataSource = myTable
            For Each row As DataRow In myTable.Rows
                row(4) = CDbl(row(4)).ToString("#,###")
            Next
            dgvList.Columns.Item(0).Width = 50
            dgvList.Columns.Item(1).Width = 85
            dgvList.Columns.Item(2).Width = 85
            dgvList.Columns.Item(3).Width = 300
            dgvList.Columns.Item(4).Width = 110
            dgvList.Columns.Item(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            dgvList.Columns.Item(5).Width = 90
            dgvList.Columns.Item(6).Width = 90
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Sub btnTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTim.Click
        UpdateList()
    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub radSoCT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radSoCT.CheckedChanged
        If radSoCT.Checked Then
            txtSoCT.ReadOnly = False
        Else
            txtSoCT.ReadOnly = True
        End If
    End Sub

    Private Sub radSoHD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radSoHD.CheckedChanged
        If radSoHD.Checked Then
            txtSoHD.ReadOnly = False
        Else
            txtSoHD.ReadOnly = True
        End If
    End Sub

    Private Sub radKH_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radKH.CheckedChanged
        If radKH.Checked Then
            cbxKhachHang.Enabled = True
        Else
            cbxKhachHang.Enabled = False
        End If
    End Sub

    Private Sub radNgayCT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radNgayCT.CheckedChanged
        If radNgayCT.Checked Then
            dtpNgayCTFrom.Enabled = True
            dtpNgayCTTo.Enabled = True
        Else
            dtpNgayCTFrom.Enabled = False
            dtpNgayCTTo.Enabled = False
        End If
    End Sub

    Private Sub radNgayHD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radNgayHD.CheckedChanged
        If radNgayHD.Checked Then
            dtpNgayHDFrom.Enabled = True
            dtpNgayHDTo.Enabled = True
        Else
            dtpNgayHDFrom.Enabled = False
            dtpNgayHDTo.Enabled = False
        End If
    End Sub
End Class